package com.cn.hnust.controller;
import com.cn.hnust.pojo.Customer;
import com.cn.hnust.service.ICustomerService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@Controller
@RequestMapping(value = "/qcustomer")
public class CustomerController {

	@Autowired
	private ICustomerService customerService;

	@RequestMapping("/selectAllCustomer")
	public void selectAllCustomer(HttpServletResponse response, Customer customer) throws IOException {
		List<Customer> list = customerService.selectAllCustomer(customer);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/getcustomerByOpenid")
	public void getcustomerByOpenid(HttpServletRequest request, Model model, Customer customer, String openid,
			HttpServletResponse response) throws IOException {

		System.out.println("openid:" + openid);
		customer = customerService.selectCustomerByOpenid(openid);
		String s = "";
		System.out.println("customer:" + customer);
		if (customer == null) {
			s = "0";
		} else {
			s = "1";
		}
		Gson gson = new Gson();
		System.out.println("s:" + s);
		String json = gson.toJson(s);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/getcustomerOpenid")
	public void getcustomerOpenid(HttpServletRequest request, Model model, Customer customer, String openid,
			HttpServletResponse response) throws IOException {
		System.out.println("openid:" + openid);
		customer = customerService.selectCustomerByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(customer);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/addcustomer")
	public void addcustomer(HttpServletRequest request, Model model, Customer customer, String openid, String cname,
			String pianhao, Integer money, String cpwd, String ctel, String cemail, String clogo,
			HttpServletResponse response) throws IOException {
		System.out.println("cname:" + cname);
		customer.setCpwd(cpwd);
		customer.setCopenid(openid);
		customer.setCname(cname);
		customer.setClogo(clogo);
		customer.setCtel(ctel);
		customer.setCemail(cemail);
		customer.setPianhao(pianhao);
		customer.setMoney(money);
		customerService.insertCustomer(customer);
		Gson gson = new Gson();
		String json = gson.toJson("");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/checkcustomer")
	public void checkcustomer(HttpServletRequest request, Model model, Customer customer, String openid, String cname,
			String cpwd, HttpServletResponse response) throws IOException {
		System.out.println("cname:" + cname);
		customer = customerService.checklogin( openid,cname, cpwd);
		Gson gson = new Gson();
		String json = gson.toJson(customer);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

	@RequestMapping("/updateCustomerByMoney")
	public void updateCustomerByMoney(HttpServletResponse response, HttpServletRequest request, Customer Customer,
			Customer cus, Integer money, String openid) throws IOException {
		System.out.println("money:" + money + " poenid:" + openid);
		cus = customerService.selectCustomerByOpenid(openid);
		int zmoney = cus.getMoney() + money;
		Customer.setId(cus.getId());
		Customer.setMoney(zmoney);
		customerService.updateByPrimaryKeySelective(Customer);
		Customer cm=customerService.selectCustomerByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(cm);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/updatecustomer")
	public void updatecustomer(HttpServletRequest request, Model model, Customer customer, String cname, String cpwd,
			String ctel, String cemail, Integer id, String openid, Integer ts, HttpServletResponse response)
			throws IOException {
		System.out.println("cname:" + cname);
		List<Customer> list = customerService.selectCustomer(cname);
		String s = "";
		if (list.size() > 0 && ts == 2) {
			s = "1";
		} else {
			customer.setCpwd(cpwd);
			customer.setCname(cname);
			customer.setCtel(ctel);
			customer.setCemail(cemail);
			customer.setId(id);
			customerService.updateCustomerById(customer);
		}
		Gson gson = new Gson();
		String json = gson.toJson(s);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
}
